var url = require('url');
var queryString = require('querystring');
var userDb = require('./userDataBaseHandler');

function authenticate(req, res) {
	var params = queryString.parse(url.parse(req.url).query);
	if (params['login'] != null && params['pass'] != null) {
		var user = userDb.getUserByLogin(params['login']);
		if (user != null) {
			// check for password
			// /!\ TODO /!\
			// HASH PASSWORD / CHECK AGAINST HASH
			if (params['pass'] == user.pass) {
				// /!\ TODO /!\
				// CREATE A REAL AUTH TOKEN
				req.session.token = user.id;
				res.end("Logged in !")
			} else {
				res.end("Log in impossible");
			}
		} else {
			res.end("Log in impossible");
		}
	} else {
		res.end("Log in impossible");
	}
}
exports.authenticate = authenticate;

function createAccount(req, res) {
	var params = queryString.parse(url.parse(req.url).query);
	if (params['login'] != null && params['pass'] != null) {
		if (userDb.getUserByLogin(params['login']) == null) {
			userDb.addUser(params['login'], params['pass']);
			res.end("User added");
		} else {
			res.end("Could not add user");
		}
	} else {
		res.end("Could not add user");
	}
}
exports.createAccount = createAccount;